using UnityEngine;

public class TP_Controller : MonoBehaviour 
{
	public static CharacterController CharacterController;
	public static TP_Controller Instance;

	void Awake() 
	{
		CharacterController = GetComponent("CharacterController") as CharacterController;
		Instance = this;
		TP_Camera.UseExistingOrCreateNewMainCamera();
	}
	
	void Update () 
	{
		if(Camera.mainCamera == null)
		{
			return;	
		}
		
		GetLocomotionInput();
		HandleActionInput();
		
		TP_Motor.Instance.UpdateMotor();
	}
	
	void GetLocomotionInput()
	{
		
		TP_Motor.Instance.VerticalVelocity = TP_Motor.Instance.MoveVector.y;
		
		if(TP_Animator.Instance.IsDead)
		{
			return;	
		}
		var deadZone = 0.1f;
		
		
		TP_Motor.Instance.MoveVector = Vector3.zero;
		
		if(Input.GetAxis("Vertical") > deadZone	|| Input.GetAxis("Vertical") < -deadZone)
		{
			TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
		}
		if(Input.GetAxis("Horizontal") > deadZone	|| Input.GetAxis("Horizontal") < -deadZone)
		{
			TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0);
		}
		
		TP_Animator.Instance.DetermineCurrentMoveDirection();
	}
	
	void HandleActionInput()
	{
		if(Input.GetButton("Jump"))
		{
			Jump();	
		}
		
		if(Input.GetKeyDown(KeyCode.E))
		{
			Use();	
		}
		
		if(Input.GetButton("Fire1"))
		{
			Attack();
		}
//		if(Input.GetKeyDown(KeyCode.Q))
//		{
//			SwordOut();	
//		}
//		
//		if(Input.GetButton("Fire1"))
//		{
//			SwingMidLeft();	
//		}
	}
	
	void Jump()
	{
		TP_Motor.Instance.Jump();	
		TP_Animator.Instance.Jump();
	}
	
	void Use()
	{
		TP_Animator.Instance.Use();
	}
	
	void Attack()
	{
		TP_Animator.Instance.Attack();
	}
	
}
